﻿using System;
using System.Windows.Forms;
using GeoFramework;
using LetsDoIt.BO;
using LetsDoIt.Interfaces;
using LetsDoIt.Common;
using LetsDoIt.Localization;

namespace LetsDoIt.Forms
{
	public partial class CaseForm : UserControl, INavigationForm
	{

		#region Implementation of INavigationForm

		public MainForm HostForm { get; set; }
		public string Identity { get; set; }
        public bool IsActive { get; set; }

		public void NavigationEnter(object sender)
		{
			//
			if (Mode == WorkMode.New)
			{
				CurrentPosition = HostForm.CurrentPosition;
			}
			else if (Mode == WorkMode.Edit)
			{
				CurrentPosition = new Position(new Latitude(Case.Latitude), new Longitude(Case.Longitude));
			}
            //
            SetPhotoCount(caseInfo.Pictures.Count);
            casePanel1.UpdateValues();
		}

		public void NavigationLeave()
		{
			//nothing
		}

		#endregion


		#region properties

		private CaseInfo caseInfo;
		public CaseInfo Case
		{
			get { return caseInfo; }
			set
			{
				caseInfo = value;
				//populate case
				casePanel1.txtDescription.Text = caseInfo.Description;
				casePanel1.trkDiameter.Value = caseInfo.Diameter;
				casePanel1.trkHeigth.Value = caseInfo.Heigth;
				casePanel1.chkDifficultAccessEx.Checked = Case.DifficultAccess;
				casePanel1.chkDifficultAccesOnRainEx.Checked = Case.DifficultAccessOnRain;
				casePanel1.trkNotRecyclable.Value = Case.NotRecyclableContent;
				casePanel1.trkGlassContent.Value = Case.GlassContent;
				casePanel1.trkMetalContent.Value = Case.MetalContent;
				casePanel1.trkPlasticContent.Value = Case.PlasticContent;
				casePanel1.trkPaperContent.Value = Case.PaperContent;
				casePanel1.trkRubberContent.Value = Case.RubberContent;
				casePanel1.trkMaterialsContent.Value = Case.MatterialsContent;
			    casePanel1.trkWidth.Value = Case.Width;
			    casePanel1.trkLength.Value = Case.Length;
			    casePanel1.chkIsDispersed.Checked = Case.IsDispersed;
                casePanel1.trkBagEq.Value = Case.BagEq;
				//populate photos
				PhotosForm frmPhotos = HostForm.GetFormByIdentity(NavigationForms.FORM_PHOTOS) as PhotosForm;
				if (frmPhotos!=null)
					frmPhotos.PhotoList = caseInfo.Pictures;
				SetPhotoCount(caseInfo.Pictures.Count);
				//
				IsDirty = false;
			}
		}

		public WorkMode Mode { get; set; }
		public bool IsDirty { get; set; }

		private Position currentPosition;
		public Position CurrentPosition
		{
			get { return currentPosition; }
			set
			{
				currentPosition = value;
				lblLat.Text = string.Format(
                    ResourceManager.Instance.GetString(@"MainForm_lblLat"),
                    currentPosition.Latitude);
				lblLng.Text = string.Format(
                    ResourceManager.Instance.GetString(@"MainForm_lblLng"),
                    currentPosition.Longitude);
			}
		}

		#endregion



		#region ctor

		public CaseForm(MainForm hostForm, string identity)
		{
			HostForm = hostForm;
			Identity = identity;
			InitializeComponent();
			casePanel1.CaseGetsDirty += new EventHandler(casePanel1_CaseGetsDirty);
		}

		void casePanel1_CaseGetsDirty(object sender, EventArgs e)
		{
			IsDirty = true;
		}

		#endregion ctor


		
		#region events

		private void btnBack_Click(object sender, EventArgs e)
		{
			if (Mode == WorkMode.New)
			{
				if(MessageBox.Show(
                    ResourceManager.Instance.GetString(@"DialogDiscardNewCaseConfirmation"),
                    ResourceManager.Instance.GetString(@"DialogConfirmation"), 
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
				{
					HostForm.NavigateBack(this, string.Empty);
				}
			}
			else
			{
				//edit mode
				if (IsDirty)
				{
					if (
						MessageBox.Show(
                        ResourceManager.Instance.GetString(@"DialogDiscardCaseUpdatesConfirmation"),
                        ResourceManager.Instance.GetString(@"DialogConfirmation"), 
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
					{
						HostForm.NavigateBack(this, string.Empty);
					}
				}
				else
				{
					HostForm.NavigateBack(this, string.Empty);
				}
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			//save here
			Case.Description = casePanel1.txtDescription.Text;
			Case.Diameter = casePanel1.trkDiameter.Value;
			Case.Heigth = casePanel1.trkHeigth.Value;
			Case.DifficultAccess = casePanel1.chkDifficultAccessEx.Checked;
			Case.DifficultAccessOnRain = casePanel1.chkDifficultAccesOnRainEx.Checked;
			Case.NotRecyclableContent = casePanel1.trkNotRecyclable.Value;
			Case.GlassContent = casePanel1.trkGlassContent.Value;
			Case.MetalContent = casePanel1.trkMetalContent.Value;
			Case.PlasticContent = casePanel1.trkPlasticContent.Value;
			Case.PaperContent = casePanel1.trkPaperContent.Value;
			Case.RubberContent = casePanel1.trkRubberContent.Value;
			Case.MatterialsContent = casePanel1.trkMaterialsContent.Value;
            Case.Width = casePanel1.trkWidth.Value;
            Case.Length = casePanel1.trkLength.Value;
            Case.IsDispersed = casePanel1.chkIsDispersed.Checked;
            Case.BagEq = casePanel1.trkBagEq.Value;
            //
			if(WastesLocalFactory.SaveLocalCase(caseInfo))
			{
				MessageBox.Show(
                    ResourceManager.Instance.GetString(@"DialogSaveLocalSuccesConfirmation"),
                    ResourceManager.Instance.GetString(@"DialogSaveConfirmation"),
                    MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
				HostForm.NavigateBack(this, string.Empty);
			}
			else
			{
				MessageBox.Show(
                    ResourceManager.Instance.GetString(@"DialogSaveCaseErrorMessage"),
                    ResourceManager.Instance.GetString(@"DialogSaveErrorTitle"),
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
			}
			
		}

		private void btnPhotoCapture_Click(object sender, EventArgs e)
		{
			HostForm.NavigateTo(this, NavigationForms.FORM_PHOTOS);
		}

		#endregion events


		#region methods

		public void SetPhotoCount(int count)
		{
			btnPhotoCapture.Caption = count>0 ?
                string.Format(ResourceManager.Instance.GetString(@"MainForm_btnPhotoCapture_Many")
                , count) :
                ResourceManager.Instance.GetString("MainForm_btnPhotoCapture");
		}

		public void ResetCase()
		{
			//
			IsDirty = false;
		}

		#endregion
	}
}
